package servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import server.ForumAPIService;
import server.controller.WebAdapter;
import database.DatabaseStub;
import forum.Forum;

/**
 * Servlet implementation class RemoveUser
 */
public class RemoveUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RemoveUser() {
		super();
	}

	/**
	 * @see Servlet#init()
	 */
	public void init() throws ServletException {

	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("RemoveUser doGet");

		// init session of request
		HttpSession session = request.getSession();

		Boolean isLoggedIn = (Boolean) session.getAttribute("isLoggedIn");
		Boolean isModerator = (Boolean) session.getAttribute("isModerator");

		if (isLoggedIn != null && isLoggedIn.booleanValue()
				&& isModerator != null && isModerator.booleanValue()) {
			ForumAPIService db = WebAdapter.getInstance();

			// get the request dispatcher
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/jsp/removeUser.jsp");

			// forward to the jsp file to display the book list
			dispatcher.forward(request, response);
		} else {// not logged in

			request.setAttribute("errorMsg",
					"You do not have enough permissions to see this page.");
			String addr = "http://" + request.getServerName() + ":"
					+ request.getServerPort() + request.getContextPath();

			request.setAttribute("errorLink", addr);
			// get the request dispatcher
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/jsp/Error.jsp");

			// forward to the jsp file to display the book list
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("RemoveUser post");
		ForumAPIService db = WebAdapter.getInstance();
		String name = request.getParameter("name");
		if (db.removeUser(name) == 0) {
			request.setAttribute("errorMsg", "Error in remove user.");
			String addr = "http://" + request.getServerName() + ":"
					+ request.getServerPort() + request.getContextPath()
					+ "/viewForums";

			request.setAttribute("errorLink", addr);
			// get the request dispatcher
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/jsp/Error.jsp");

			// forward to the jsp file to display the book list
			dispatcher.forward(request, response);
		} else {
			response.sendRedirect(request.getContextPath() + "/viewForums");
		}
	}

}
